﻿/*
   Copyright 2013 Dmitry Bratus

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;

namespace Flower.Workflow
{
    /// <summary>
    /// Base class of all workflows.
    /// </summary>
    [DataContract(Namespace = XmlNamespace.Value)]
    public abstract class Process
    {
        [DataContract(Namespace = XmlNamespace.Value)]
        internal class ForEachData
        {
            [DataContract(Namespace = XmlNamespace.Value)]
            internal class MessageInfo
            {
                [DataMember]
                internal string Id { get; set; }

                [DataMember]
                internal string Name { get; set; }

                [DataMember]
                internal byte[] Data { get; set; }

                [DataMember]
                internal int DataFormat { get; set; }
            }

            /// <summary>
            /// How many items were removed in the current batch.
            /// </summary>
            [DataMember]
            internal int RemovalsCount { get; set; }

            /// <summary>
            /// Offset of the current batch.
            /// </summary>
            [DataMember]
            internal long Offset { get; set; }
        
            /// <summary>
            /// Index in the current batch.
            /// </summary>
            [DataMember]
            internal int Index { get; set; }

            /// <summary>
            /// The current batch of messages.
            /// </summary>
            [DataMember]
            internal List<MessageInfo> Batch { get; set; }

            /// <summary>
            /// Id of the current message.
            /// </summary>
            [DataMember]
            internal string MessageId { get; set; }

            internal ForEachData()
            {
                Batch = new List<MessageInfo>();
            }

            [OnDeserialized]
            protected void OnDeserialized(StreamingContext context)
            {
                if (Batch == null)
                {
                    Batch = new List<MessageInfo>();
                }
            }
        }

        /// <summary>
        /// The current position in the activities sequence.
        /// </summary>
        [DataMember]
        internal int __ActivityPointer { get; set; }

        /// <summary>
        /// How many times the current activity was executed.
        /// </summary>
        [DataMember]
        internal int __RetriesCount { get; set; }

        /// <summary>
        /// Paths/ids of the wait links created by a process.
        /// </summary>
        [DataMember]
        internal List<string> __WaitLinks { get; private set; }

        /// <summary>
        /// Stack of the ForEach loops data.
        /// </summary>
        [DataMember]
        internal List<ForEachData> __ForEachData { get; private set; } 

        /// <summary>
        /// The default constructor.
        /// </summary>
        protected Process()
        {
            __WaitLinks = new List<string>();
            __ForEachData = new List<ForEachData>();
        }

        /// <summary>
        /// Deserialization handler.
        /// </summary>
        /// <param name="context">The streaming context.</param>
        [OnDeserialized]
        protected void OnDeserialized(StreamingContext context)
        {
            if (__WaitLinks == null)
            {
                __WaitLinks = new List<string>();
            }

            if (__ForEachData == null)
            {
                __ForEachData = new List<ForEachData>();
            }
        }
    }
}
